package com.yishu.servlet;

import com.yishu.Dao.UserDao;
import com.yishu.util.DBConnection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;

public class UpdateUserServlet extends HttpServlet{

    UserDao userDao = new UserDao();
    DBConnection dbConnection = new DBConnection();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int userId=Integer.parseInt(req.getParameter("userId"));
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
        String email = req.getParameter("email");

        try {
            Connection connection = dbConnection.getCon();
            int result = userDao.updateUser(userId,userName,password,email,connection);
            dbConnection.closeCon(connection);

            if(result == 1) {
                req.setAttribute("result","更新成功!");
                req.getRequestDispatcher("updateResult.jsp").forward(req,resp);
            }else {
                req.setAttribute("result","更新失败!");
                req.getRequestDispatcher("updateResult.jsp").forward(req,resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
